#include<bits/stdc++.h>
using namespace std;
int T,n,f[1000010],g[1000010],mxf,mxg,tf,tg,ans,a[1000010];
long long InF=0;
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=1000000;i++) f[i]=InF,g[i]=InF;
		mxf=0,mxg=0,tf=0,tg=0;
		f[0]=0,g[0]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			int x=max(mxf,f[a[i]]+a[i]+tf);
			int y=max(mxg,g[a[i]]+a[i]+tg);
			if(a[i]==a[i-1]) tf=a[i],tg=a[i],mxf+=a[i],mxg+=a[i];
			mxf=max(mxf,y);f[a[i-1]]=max(f[a[i-1]],y-tf);
			mxg=max(mxg,x);g[a[i-1]]=max(g[a[i-1]],x-tg);
		}
		for(int i=0;i<=1000000;i++) ans=max(ans,f[i]),ans=max(ans,g[i]);
		cout<<max(mxf,mxg)<<endl;
	}
	return 0;
}
